import numpy as np
import operator
import matplotlib
import matplotlib.pyplot as plt

def createDateSet():
    group = np.array([[1.0,1.0],[1.0,1.1],[0,0],[0.0,0.1]])
    label = ['A','A','B','B']
    return group,label

def classify0(inx,dataSet,label,k):
    setSize = dataSet.shape[0]#获取np.array 的(n,m)
    diffMat = np.tile(inx,(setSize,1)) - dataSet#计算距离
    mpDiffMat = diffMat**2
    mpDistances = np.sum(mpDiffMat,axis = 1)
    distances = mpDistances**0.5
    sortedDis = np.argsort(distances)#距离排序
    count = {}
    for i in range(k):
        voteLabel = label[sortedDis[i]]
        count[voteLabel] = count.get(voteLabel,0)+1
    sortedCount = sorted(count.items(),key=operator.itemgetter(1),reverse=True)#count数最多的在前面
    print(sortedCount)
    return sortedCount[0][0]

#读取文件获取数据集
def file2matrix(filename):
    f = open(filename)
    arraylines = f.readlines()
    linecount = len(arraylines)
    returnMat = np.zeros((linecount,3))
    labels = []
    index = 0
    for line in arraylines:
        line = line.strip()
        linearray = line.split('\t')
        returnMat[index,:] = linearray[0:3]
        labels.append(int(linearray[-1]))
        index += 1
    return returnMat,labels

def autoNorm(dataSet):
    minVal = dataSet.min(0)
    maxVal = dataSet.max(0)
    ranges = maxVal - minVal
    normDataSet = np.zeros(np.shape(dataSet))
    m = dataSet.shape[0]
    normDataSet = dataSet - np.tile(minVal,(m,1))#铺瓷砖
    normDataSet = normDataSet/np.tile(ranges,(m,1))
    return normDataSet,ranges,minVal

def datingClassTest():
    

if __name__ == '__main__':
    dataMat,dataLabel = file2matrix("datingTestSet2.txt")
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    normDataSet,ranges,minVal = autoNorm(dataMat)
    ax.scatter(normDataSet[:,1],normDataSet[:,2],15.0*np.array(dataLabel),15.0*np.array(dataLabel))
    plt.show()
    #print(normDataSet,ranges,minVal)